using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Unity.VisualScripting;
using UnityEngine;

public class Player_Control : MonoBehaviour
{
    GameObject UI;
    //玩家
    Rigidbody2D Rb;//刚体
    GameObject Weapon;//武器
    PlayerUI_control Ui;//自身Ui控制
    Play_Animation Ani;//动画
    public int Dir = 1;
    float hurt;
    Vector3 v;//初始位置
    //////
    bool IsJump = false;
    //检测是否碰地，如果碰地，不处于跳跃状态，IsJump = false
    void Start()
    {
        hurt = 10;
        Rb = GetComponent<Rigidbody2D>();
        Weapon = transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).GetChild(3).GetChild(1).GetChild(0).GetChild(1).gameObject;
        UI = transform.GetChild(1).gameObject;
        Ui = UI.transform.GetComponent<PlayerUI_control>();
        Ani = GetComponent<Play_Animation>();
        v = transform.position;
    }
    void Update()
    {
        Player_Move();
        if (Input.GetKeyDown(KeyCode.J))
        {
            Weapon.GetComponent<Player_Weapon>().SumHurt(hurt);
            StartCoroutine(Attack(0));
        }
        if (Input.GetKeyDown(KeyCode.K))
        {            
            Weapon.GetComponent<Player_Weapon>().SumHurt(hurt*2);
            StartCoroutine(Attack(1));
        }
        if (Input.GetKeyDown(KeyCode.L))
        {
            StartCoroutine(Attack(4));
        }       
        if (Input.GetKeyDown(KeyCode.U))
        {
            Ui.AddHP(20);
            //Ui.DescreaseHP(50);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //GameObject.Find("UI").GetComponent<UI>().IsContinue = !GameObject.Find("UI").GetComponent<UI>().IsContinue;
        }
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "ground"||other.tag=="boss")
        {
            IsJump = false;
        }
    }
    IEnumerator Attack(int index)
    {
        Ani.Attack(index);
        yield return new WaitForSeconds(0.2f);
        Weapon.GetComponent<BoxCollider2D>().enabled = true;
        yield return new WaitForSeconds(0.04f);
        Weapon.GetComponent<BoxCollider2D>().enabled = false;
    }
    public void Be_attack(int dir,float h)
    {
        //Rb.AddForce(new Vector2(4*dir,4),ForceMode2D.Impulse);
        Rb.velocity = new Vector3(-dir * 5, 3, 0);
        Ui.DescreaseHP(h);
    }
    public void Death()
    {
        Ani.Death();
    }
    private void Player_Move()
    {
        //角色控制
        if (GameObject.Find("UI").GetComponent<UI>().IsContinue)
        {
            if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
            {
                Ani.Move();
                transform.position += new Vector3(Input.GetAxis("Horizontal") * Time.deltaTime * 5, Input.GetAxis("Vertical") * Time.deltaTime * 5, 0);
                if (Input.GetKeyDown(KeyCode.A))
                {
                    Dir = 1;
                    transform.localScale = new Vector3(Dir, 1, 1);
                    //UI.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
                }
                if (Input.GetKeyDown(KeyCode.D))
                {
                    Dir = -1;
                    transform.localScale = new Vector3(Dir, 1, 1);
                    //UI.transform.localScale = new Vector3(-0.01f, 0.01f, -0.01f);
                }
            }
            else
            {
                Ani.Idle();
            }
            if (Input.GetKeyDown(KeyCode.W) && !IsJump)
            {
                Rb.AddForce(new Vector2(0, 4.5f), ForceMode2D.Impulse);
                //rb.velocity = new Vector2(rb.velocity.x, 5);
                IsJump = true;
            }            
        }
    }
    public void Player_Reset()
    {
        this.gameObject.SetActive(true);
        transform.position = v;
        Ui.PlayerUI_Reset();
    }
}
